home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 July / EnigmA AMIGA RUN 20 (1997)(G.R. Edizioni)(IT)[!][issue 1997-07 & 08][EAR-CD IV].iso / earcd / comm / mmgr / mm_searc.lha / Rexx / MM_SearchMsgs.rexx
OS/2 REXX Batch file  |  1997-03-15  |  10KB  |  370 lines

  1. /*
  2.  
  3.                         $VER: MM_SearchMsgs  1.0  (15-3-97)
  4.  
  5.                             ©1997 Neil Williams
  6.  
  7. */
  8.  
  9. parse arg args
  10.  
  11. options cache
  12. options failat 99
  13. options results
  14.  
  15. signal on break_c
  16. signal on break_d
  17. signal on break_e
  18. signal on break_f
  19. signal on halt
  20. signal on ioerr
  21. signal on syntax
  22.  
  23. address 'MAILMANAGER'
  24.  
  25.  
  26. Main:
  27.  
  28.         call Init
  29.         call Parse_Args(strip(args))
  30.         call Header
  31.  
  32.         call SearchMsgs
  33.  
  34.  
  35. call Quit(0, 'All done.')
  36. exit
  37.  
  38.  
  39. break_c:; break_d:; break_e:; break_f:; halt:
  40.  
  41.         signal off break_c
  42.         signal off break_d
  43.         signal off break_e
  44.         signal off break_f
  45.         signal off halt
  46.  
  47.         return_code             =       5
  48.         error_line    = 0
  49.         error_msg                       = 'Execution halted!!!'
  50.         rc                                              = 0
  51. signal Exit
  52.  
  53.  
  54. Exit:
  55.  
  56.         select
  57.                 when return_code>=40 then error = 'INTERNAL-ERROR:'
  58.                 when return_code>=30 then error = 'IO-ERROR:'
  59.                 when return_code>=20 then error = 'ERROR:'
  60.                 when return_code>=10 then error = 'WARNING:'
  61.                 when return_code>=5  then error = 'INFO:'
  62.                 otherwise                                                                       error = ''
  63.         end
  64.  
  65.   call Log()
  66.         call Log('***' strip(error error_msg) '***', '+')
  67.         call Log(,'\')
  68.  
  69.         call setclip('MM_LogPre', system.mm.logpre)
  70.  
  71. exit return_code
  72.  
  73.  
  74. Get_Arg: procedure Expose args system.
  75.  
  76.   arg keyword, mode, old
  77.  
  78.         uargs   = upper(args)
  79.         p                       = find(uargs, keyword)
  80.  
  81.         if p=0  then
  82.                 do
  83.                         p = pos(' 'keyword'=', ' 'uargs)
  84.  
  85.                         if p>0 then     args    = overlay(' ', args, p+length(keyword))
  86.  
  87.       p = find(upper(args), keyword)
  88.                 end
  89.  
  90.         system.cmdopt.keyword   = p>0
  91.  
  92.         select
  93.                 when mode=0     then
  94.                         if p>0 then
  95.                                 do
  96.                                         ret             = 1
  97.                                         args    = delword(args, p, 1)
  98.                                 end
  99.                         else ret        = old
  100.  
  101.                 when mode=1 then
  102.                         if p>0 then
  103.                                 do
  104.                                         left    = subword(args, 1, p-1)
  105.                                         rest    = subword(args, p+1)
  106.  
  107.                                         if left(rest, 1)='"' then       parse var rest . '"'    ret '"' rest
  108.                                         else                                                                                    parse var rest                          ret                     rest
  109.  
  110.                                         args    = strip(left strip(rest))
  111.                                 end
  112.                         else ret        = old
  113.  
  114.                 when mode=2 then
  115.                         do
  116.                                 if left(args, 1)='"'    then    parse var args . '"'    ret '"' args
  117.                                 else                                                                                            parse var args                          ret             args
  118.  
  119.                                 if strip(ret)=''                        then    ret = old
  120.                         end
  121.  
  122.                 otherwise exit 99
  123.         end
  124.  
  125.         args    = strip(args)
  126.         ret             = strip(ret, 'b', '" ')
  127.  
  128. return ret
  129.  
  130.  
  131. Get_Version: procedure
  132.  
  133.         parse arg mode
  134.  
  135.         parse value sourceline(3-mode) with . . ver .
  136.         parse var ver tst 'ß' .
  137.  
  138.         if ~datatype(strip(tst, 'b', '/c '), 'N') then
  139.                 if ~mode then ver = Get_Version(1)
  140.                 else exit 99
  141.  
  142. return ver
  143.  
  144.  
  145. Header:
  146.  
  147.         call Log(,'/')
  148.         call Log('***' system.prg.id '***', '+')
  149.         call Log(' 'system.prg.cr)
  150.         call Log()
  151.  
  152. return
  153.  
  154.  
  155. Init:
  156.  
  157.         system.                                         = 0
  158.  
  159.         system.prg.ver          = Get_Version(0)
  160.         system.prg.name         = 'MM_SearchMsgs'
  161.         system.prg.id           = system.prg.name 'v'system.prg.ver
  162.         system.prg.cfg          = 'MM:Config/'system.prg.name'.cfg'
  163.         system.prg.cr           = '©1997 Neil Williams'
  164.         system.tmpfile          = 'T:'system.prg.name'.tmp'
  165.         system.mm.logpre        = getclip('MM_LogPre')
  166.         system.prg.logpre       = system.mm.logpre'|'
  167.         system.prg.loglevel     = 3
  168.         call                                                            setclip('MM_LogPre', system.prg.logpre)
  169.         system.cmdopts          = 'FROM/K,TO/K,SUBJECT/K,FILE/K'
  170.  
  171.         call Include_Lib('rexxsupport')
  172. return
  173.  
  174.  
  175. Include_Lib: procedure Expose system.
  176.  
  177.         parse arg lib, prio
  178.         if right(upper(lib), 8)~='.LIBRARY' then lib    = lib'.library'
  179.         if prio=''           then prio       = 0
  180.  
  181.         if ~show('l', lib) then
  182.                 if ~addlib(lib, prio, -30, 0) then call Quit(20, 'Could not open' lib'!!!')
  183.  
  184. return
  185.  
  186.  
  187. IOerr:
  188.  
  189.         signal off ioerr
  190.  
  191.         return_code             = 20
  192.         error_line    = sigl
  193.         error_msg                       = 'IO-error' rc 'at line' sigl '['errortext(rc)']')
  194.         rc                                              = 0
  195. signal Exit
  196.  
  197.  
  198. Log: procedure Expose system.
  199.  
  200.         parse arg text, pre, level
  201.  
  202.         if ~datatype(level, 'N') then level = system.prg.loglevel
  203.  
  204.         tmp             = word('PRG MM', (pre~='')+1)
  205.         text    = system.tmp.logpre || pre' 'text
  206.  
  207.         MM_WriteLog 'text' level
  208. return
  209.  
  210.  
  211. Parse_Args: procedure Expose system.
  212.  
  213.         parse arg args
  214.  
  215.         tpl             = system.cmdopts',?/S'
  216.         args    = translate(args, '  ', '9'x'=')
  217.  
  218.         pk              = pos('/K', tpl)
  219.         ps              = pos('/S', tpl)
  220.  
  221.         select
  222.                 when pk=0       & ps=0  then    p       = 0
  223.                 when pk=0 & ps>0        then  p = ps
  224.                 when ps=0 & pk>0        then    p       = pk
  225.                 otherwise                                                               p       = min(pk, ps)
  226.         end
  227.  
  228.         p                       = lastpos(',', left(tpl, p))
  229.         tpl             = substr(tpl, p+1) || left(tpl, max(p-1, 0))
  230.  
  231.         do while tpl~=''
  232.                 parse var tpl template ',' tpl
  233.                 parse var template keyword '/' .
  234.  
  235.                 bool    = pos('/S',     template)>0
  236.                 key             = pos('/K', template)>0
  237.                 must    = pos('/A', template)>0
  238.                 num             = pos('/N', template)>0
  239.  
  240.                 select
  241.                         when must then          system.arg.keyword      = '0'x
  242.                         when bool       then            system.arg.keyword      = 0
  243.                         when num        then            system.arg.keyword      = 0
  244.  
  245.                         otherwise                                       system.arg.keyword      = ''
  246.                 end
  247.  
  248.                 if bool | key   then    mode    = ~bool
  249.                 else                                                            mode    = 2
  250.  
  251.                 system.arg.keyword      = Get_Arg(keyword, mode, system.arg.keyword)
  252.  
  253.                 if keyword='?' & system.arg.keyword=1 then leave
  254.  
  255.                 if must & system.arg.keyword='0'x then
  256.                         do
  257.                                 tmp     = template 'missing!!!'
  258.  
  259.                                 say
  260.                                 say ' ***' tmp '***'
  261.  
  262.                                 signal Usage
  263.                         end
  264.  
  265.                 if num & ~datatype(system.arg.keyword, 'N') then
  266.                         if ~must & system.arg.keyword='' then   system.arg.keyword      = 0
  267.                         else
  268.                                 do
  269.                                         tmp     = 'Numeric value expected for' template', but is "'system.arg.keyword'"!!!'
  270.  
  271.                                         say
  272.                                         say ' ***' tmp '***'
  273.  
  274.                                         signal Usage
  275.                                 end
  276.         end
  277.  
  278.         tmp     = '?'; if system.arg.tmp then signal Usage
  279.  
  280.         if args~='' then call Quit(10, 'Unknown option(s):' args)
  281.  
  282.  
  283.         /* additional checks... */
  284.  
  285.  
  286. return
  287.  
  288.  
  289. Quit:
  290.  
  291.         parse arg return_code, error_msg
  292.  
  293.         error_line    = 0
  294.         rc                                              = 0
  295. signal Exit
  296.  
  297.  
  298.  
  299.  
  300. Syntax:
  301.  
  302.         signal off syntax
  303.  
  304.         return_code             = 40
  305.         error_line    = sigl
  306.         error_msg                       = 'Syntax-error' rc 'at line' sigl '['errortext(rc)']'
  307.         rc                                              = 0
  308. signal Exit
  309.  
  310.  
  311. Usage:
  312.  
  313.         rx.                     = ''
  314.         rx.0.0  = '[rx] '
  315.         rx.0.1  = '[.rexx]'
  316.         m                               =       pos('/e', system.prg.ver)>0
  317.  
  318.         say
  319.         say 'Usage:' rx.m.0 || system.prg.name || rx.m.1 system.cmdopts
  320.         say
  321. call Quit(0, 'Usage requested.')
  322.  
  323.  
  324.  
  325.  
  326. SearchMsgs:
  327.  
  328.     call open(fh,system.arg.file,"W")
  329.  
  330.     drop AREAS. SEARCH. RMSG.
  331.     MM_GetAreas AREAS
  332.  
  333.     do i = 0 to AREAS.COUNT-1
  334.  
  335.        call Log('Searching area : 'AREAS.i)
  336.  
  337.        MM_SearchMsgs AREAS.i SEARCH '"'system.arg.from'"' '"'system.arg.to'"' '"'system.arg.subject'"'
  338.  
  339.        if SEARCH.COUNT ~= 0 then call Log(' ... found 'SEARCH.COUNT' messages')
  340.        else call Log(' ... no messages found')
  341.  
  342.        do j = 0 to SEARCH.COUNT-1
  343.           MM_ReadMsg AREAS.i SEARCH.j RMSG
  344.  
  345.           writeln(fh,' ')
  346.           writeln(fh,'-----------------------------------------------------------------------------')
  347.           writeln(fh,'From:    'RMSG.FROM' 'RMSG.FROMADDR'  To: 'RMSG.TO' 'RMSG.TOADDR)
  348.           writeln(fh,'Subject: 'RMSG.SUBJ'    Date: 'RMSG.DATE'    AREA: 'AREAS.i)
  349.           writeln(fh,'-----------------------------------------------------------------------------')
  350.  
  351.           do k = 0 to RMSG.HEAD.COUNT-1
  352.              writeln(fh,RMSG.HEAD.k)
  353.              end
  354.           do k = 0 to RMSG.TEXT.COUNT-1
  355.              writeln(fh,RMSG.TEXT.k)
  356.              end
  357.           do k = 0 to RMSG.FOOT.COUNT-1
  358.              writeln(fh,RMSG.FOOT.k)
  359.              end
  360.  
  361.           writeln(fh,' ')
  362.           end /* do j */
  363.  
  364.        end /* do i */
  365.  
  366.     call close(fh)
  367.  
  368. return
  369.  
  370.